package com.uenpay.im.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.qyx.android.message.type.PacketType;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.contacts.SortModel;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.RoundImageView;

import java.util.List;

public class SortAdapter extends BaseAdapter implements SectionIndexer {

	private List<SortModel> list = null;

	private Context mContext;

	public SortAdapter(Context mContext, List<SortModel> list) {
		this.mContext = mContext;
		this.list = list;
	}

	public void setData(List<SortModel> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		final SortModel mSortModel = list.get(position);
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_sort_list_layout, parent, false);
			viewHolder.avatarIv = (RoundImageView) convertView
					.findViewById(R.id.user_avatar);
			viewHolder.tvLetter = (TextView) convertView
					.findViewById(R.id.catalog);
			viewHolder.nameTv = (TextView) convertView
					.findViewById(R.id.user_name);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		// ����position��ȡ���������ĸ��Char asciiֵ
		int section = getSectionForPosition(position);
		// �����ǰλ�õ��ڸ÷�������ĸ��Char��λ�� ������Ϊ�ǵ�һ�γ���
		if (position == getPositionForSection(section)) {
			viewHolder.tvLetter.setVisibility(View.VISIBLE);
			viewHolder.tvLetter.setText(mSortModel.getSortLetters());
		} else {
			viewHolder.tvLetter.setVisibility(View.GONE);
		}
		if (!TextUtils.isEmpty(mSortModel.getmFriendEntity().remarks_name)) {
			viewHolder.nameTv
					.setText(mSortModel.getmFriendEntity().remarks_name);
		} else {
			viewHolder.nameTv.setText(mSortModel.getmFriendEntity().nick_name);
		}
		viewHolder.avatarIv.SetUrl(APIConfiguration.getAvatarUrlNormal(
				mSortModel.getmFriendEntity().cust_id, PacketType.PERSONAL));
		return convertView;
	}

	@Override
	public Object[] getSections() {
		return null;
	}

	@Override
	public int getPositionForSection(int sectionIndex) {
		for (int i = 0; i < getCount(); i++) {
			String sortStr = list.get(i).getSortLetters();
			char firstChar = sortStr.toUpperCase().charAt(0);
			if (firstChar == sectionIndex) {
				return i;
			}
		}

		return -1;
	}

	@Override
	public int getSectionForPosition(int position) {
		return list.get(position).getSortLetters().charAt(0);
	}

	final static class ViewHolder {
		private TextView tvLetter;
		private RoundImageView avatarIv;
		private TextView nameTv;
	}

}
